﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DBSourceToolsLib
{
    public class SourceDatabaseLoader : Interfaces.IProgressEvent, Interfaces.ISourceDatabaseCommand
    {
        SourceDatabase m_SourceDatabase;
        private string m_Params = "";
        Form m_ParentForm;
        Commands.DatabaseCommandTypes m_CommandType;

        private bool m_HasErrors = false;
        private string m_ErrorMessage = null;
        private string m_strNewQueryFileName = null;
        public string NewQueryFileName
        {
            get { return m_strNewQueryFileName; }
        }
        public string NewQueryName
        {
            get {
                string strShortName = m_strNewQueryFileName.Substring(m_strNewQueryFileName.LastIndexOf('\\') + 1);
                strShortName = strShortName.Substring(0, strShortName.LastIndexOf('.'));
                return strShortName;

            }
        }

        public SourceDatabaseLoader(Form oForm
            , Commands.DatabaseCommandTypes oCommandType)
        {
            m_ParentForm = oForm;
            m_CommandType = oCommandType;
        }

        #region IProgressEvent Members

        public event EventHandler<ProgressEventArgs> ProgressEventPublisher;

        public void Execute()
        {
            EventHandler<ProgressEventArgs> localHandler = ProgressEventPublisher;

            switch (m_CommandType)
            {
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDisk:
                    m_SourceDatabase.SysObjects.LoadFromDisk();
                    m_SourceDatabase.SysObjects.LoadDependencyTreeFromDisk();
                    m_SourceDatabase.ScriptOptions.LoadScriptOptionsFromDisk(localHandler);
                    //m_SourceDatabase.SysObjects.LoadDeploymentTargets(this.m_Database.BaseDirectory);
                    break;
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDatabase:
                    m_SourceDatabase.ClearFilesFromDisk();
                    m_SourceDatabase.SysObjects.LoadFromDb(localHandler);
                    m_SourceDatabase.SysObjects.LoadDependencyTreeFromDb(localHandler);
                    m_SourceDatabase.ScriptOptions.LoadScriptOptionsFromDisk(localHandler);
                    if (m_SourceDatabase.SysObjects.SODependencyTree.HasErrors())
                    {
                        DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                            new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(m_SourceDatabase.SysObjects.SODependencyTree.GetErrors());
                        oError.Show();

                    }
                    break;
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.CountDataQuery:
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.NewQuery:
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.NewProc:
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.NewFunction:
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.NewDatabase:
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.NewTableQuery:
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.NewTableFromExcel:
                case Commands.DatabaseCommandTypes.NewView:
                case Commands.DatabaseCommandTypes.NewQueryScriptInsert:
                case Commands.DatabaseCommandTypes.NewQueryScriptSelect:
                case Commands.DatabaseCommandTypes.NewQuerySelectView:
                case Commands.DatabaseCommandTypes.NewQueryUpdateTable:
                    m_strNewQueryFileName = m_SourceDatabase.SysObjects.NewQuery( m_CommandType, m_Params);
                    break;
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.CompareDatabases:
                    {
                        m_SourceDatabase.CompareDatabase(localHandler, m_Params);
                        //m_SourceDatabase.SysObjects.NewQuery(
                        //    DBSourceToolsLib.Commands.DatabaseCommandTypes.SynchronizationScript, strText);
                    }
                    break;
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadQueriesFromDisk:
                    m_SourceDatabase.SysObjects.ReLoadQueriesFromDisk();
                    m_SourceDatabase.LoadPatchesFromDisk();
                    break;
                case DBSourceToolsLib.Commands.DatabaseCommandTypes.NewPatch:
                    //m_strNewQueryFileName = m_SourceDatabase.NewPatch(m_Params);
                    break;
            }

            if (localHandler != null)
                localHandler(this, new ProgressEventArgs(100, 100,
                    "Processing Complete"));

        }

        #endregion

        #region ISourceDatabaseCommand Members

        public void Execute(ref SourceDatabase oRefSourceDatabase, string strParams)
        {
            try
            {
                m_SourceDatabase = oRefSourceDatabase;
                m_Params = strParams;
                UI.Forms.ProgressForm oProgressForm = new DBSourceToolsLib.UI.Forms.ProgressForm();
                oProgressForm.setEventSource(this);
                oProgressForm.ShowDialog(m_ParentForm);
                if (!String.IsNullOrEmpty(oProgressForm.ErrorMessage))
                    throw new Exception(oProgressForm.ErrorMessage);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        #endregion

        #region IProgressEvent Members


        public bool HasErrors()
        {
            return m_HasErrors;
        }

        public string GetErrorMessage()
        {
            return m_ErrorMessage;
        }

        #endregion

        #region IProgressEvent Members


        public void RegisterError(string strErrorMessage)
        {
            m_HasErrors = true;
            m_ErrorMessage = strErrorMessage;
        }

        #endregion
    }
}
